<script>
import Footer from "../../components/Footer.vue"
import request from "@/utils/request"
import { currentUser, logoutUser } from "@/utils/login-user"
import UserNavigator from "@/components/UserNavigator.vue"
import SearchBar from "@/components/SearchBar.vue"

const posters = [
    require("../../assets/index/poster0.jpg"),
    require("../../assets/index/poster1.jpg"),
    require("../../assets/index/poster2.jpg")
]

class TimerTask {

    constructor(func, delay){
        this.func = func;
        this.delay = delay;
    }
    schedule() {
        if(this.handle){
            throw new Error("already start")
        }
        this.handle = setInterval(() => this.func(), this.delay)
    }

    restart(){
        if(this.handle){
            clearInterval(this.handle)
            this.handle = setInterval(() => this.func(), this.delay)
        }else{
            throw new Error("not start")
        }
    }
}



export default {
    components: {
        Footer,
        UserNavigator,
        SearchBar
    },
    data() {
        return {
            poster: {
                index: 0,
                uri: posters[0],
                hover: false
            },
            cursor:{
                hover: false
            },
            user: {},
            bagUpdate: 0,
            search: '',
            itemData: []
        };
    },
    created() {
        this.user = currentUser();
        this.search = this.$route.query.search
        this.loadData();
        this.timerTask = new TimerTask(() => {
            this.poster.index = (this.poster.index + 1) % posters.length;
            this.poster.uri = posters[this.poster.index]
        }, 3000)
        this.timerTask.schedule()
    },
    methods: {
        routeRegist() {
            this.$router.push("/register");
            return;
        },
        routeLogin() {
            this.$router.push("/login");
            return;
        },
        routeAdmin() {
            this.$router.push('/user');
            return;
        },
        logout() {
            logoutUser()
        },
        _search(value) {
            this.search = value
            this.loadData()
        },
        routeItem(itemId) {
            const url = '/shop/detail?id=' + itemId;
            this.$router.push(url);
        },
        addCartItem(id) {
            request.post("/cart", {
                itemId: id,
                quantity: 1,
            }).then((res) => {
                this.bagUpdate++;
            })
        },
        showShoppingCart(index) {
            this.$set(this.itemData[index], 'showCar', true)
        },
        hideShoppingCart(index) {
            this.$set(this.itemData[index], 'showCar', false)
        },
        userlogout() {
            this.bagUpdate++;
        },
        loadData() {
            request.get('/item', {
                params: {
                    search: this.search,
                }
            }).then(res => {
                this.itemData = res.data.records;
            })
        },
        nextPoster(diff){
            let newIndex = (this.poster.index + diff) % posters.length;
            newIndex = newIndex < 0 ? posters.length - 1 : newIndex;
            this.switchPoster(newIndex);
        },
        switchPoster(index){
            this.poster.index = index
            this.poster.uri = posters[index]
            this.timerTask.restart()
        }
    }
}

</script>

<template>
    <main>
        <UserNavigator @logout="userlogout" />
        <SearchBar :search="_search" :bag-update="bagUpdate" :category-show="true"/>
        <div class="main-view">
            <div class="poster-container">
                <div class="poster-lantern" @mouseenter="poster.hover = true" @mouseleave="poster.hover = false">
                    <img :src="poster.uri" />
                    <div @click="nextPoster(-1)" @mouseenter="cursor.hover = true" @mouseleave="cursor.hover = false" v-show="poster.hover" :class="['switch-cursor','switch-cursor-left', cursor.hover ? 'switch-cursor-active':'']">
                        <span class="el-icon-arrow-left"></span>
                    </div>
                    <div @click="nextPoster(1)" @mouseenter="cursor.hover = true" @mouseleave="cursor.hover = false" v-show="poster.hover" :class="['switch-cursor','switch-cursor-right', cursor.hover ? 'switch-cursor-active':'']">
                        <span class="el-icon-arrow-right"></span>
                    </div>
                    <div class="switch-bar">
                        <div @click="switchPoster(0)" :class="{'switch-btn' : true, 'active-btn': poster.index == 0}"></div>
                        <div @click="switchPoster(1)" :class="{'switch-btn' : true, 'active-btn': poster.index == 1}"></div>
                        <div @click="switchPoster(2)" :class="{'switch-btn' : true, 'active-btn': poster.index == 2}"></div>
                    </div>
                </div>
                <div class="poster-plate">
                    <img src="../../assets/index/plat0.jpg" />
                    <img src="../../assets/index/plat1.jpg" />
                    <img src="../../assets/index/plat2.jpg" />
                </div>
            </div>
        </div>

        <div class="brand-view">
            <div class="brand-title">
                <div class="brand-play"/>
                <div class="brand-links">
                    <span>热门品牌</span>
                    <span>独家品牌</span>
                    <span>全部品牌</span>
                </div>
            </div>
            <div class="brand-container">
                <img src="../../assets/index/brand-main.jpg"/>
                <div class="brand-graid">
                    <div><img src="../../assets/index/brand-items-0.png"/></div>
                    <div><img src="../../assets/index/brand-items-0.png"/></div>
                    <div><img src="../../assets/index/brand-items-0.png"/></div>
                    <div><img src="../../assets/index/brand-items-0.png"/></div>
                    <div><img src="../../assets/index/brand-items-0.png"/></div>
                    <div><img src="../../assets/index/brand-items-0.png"/></div>
                    <div><img src="../../assets/index/brand-items-0.png"/></div>
                    <div><img src="../../assets/index/brand-items-0.png"/></div>
                    <div><img src="../../assets/index/brand-items-0.png"/></div>
                    <div><img src="../../assets/index/brand-items-0.png"/></div>
                    <div><img src="../../assets/index/brand-items-0.png"/></div>
                    <div><img src="../../assets/index/brand-items-0.png"/></div>
                    <div><img src="../../assets/index/brand-items-0.png"/></div>
                    <div><img src="../../assets/index/brand-items-0.png"/></div>
                    <div><img src="../../assets/index/brand-items-0.png"/></div>
                    <div><img src="../../assets/index/brand-items-0.png"/></div>
                </div>
            </div>
        </div>
        <Footer />
        <!-- <div class="body-container">
            <div class="index-head">
                <div class="item-category">

                </div>

            </div>
            <div class="product-container">
                <div class="product-item" v-for="(item, index) in itemData" @mouseover="showShoppingCart(index)"
                    @mouseout="hideShoppingCart(index)" @click.stop="routeItem(item.id)">
                    <div class="item-image">
                        <img :src="item.image" />
                        <div v-show="item.showCar" @click.stop="addCartItem(item.id)" class="car-button"><i
                                class="el-icon-shopping-bag-2"></i>加入购物车</div>
                    </div>
                    <span class="item-label">独家</span>
                    <span class="item-name">{{ item.title }}</span>
                    <span class="item-price">￥{{ item.price }}</span>

                </div>
            </div>
        </div> 
        <Footer />-->
    </main>
</template>

<style scoped>
main {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
}

.main-view {
    width: 60%;
    height: 550px;
    align-self: center;
}

.brand-view{
    width: 60%;
    height: 360px;
    display: flex;
    flex-wrap: wrap;
    margin: 40px 0;

    .brand-title{
        width: 100%;
        height: 36px;
        margin-bottom: 4px;
        display: flex;
        justify-content: space-between;

        .brand-play{
            background-image: url("../../assets/index/sprite-title.png");
            background-position: -20px -10px;
            height: 36px;
            width: 380px;
            background-size: 100%;
        }

        .brand-links{
            & span{
                font-size: 15px;
                margin-left: 10px;
                cursor: pointer;

                &:hover{
                    font-weight: bold;
                }
            }
        }
        
    }

    .brand-container{
        display: flex;
        height: 320px;
        width: 100%;

        &>img{
            width: 590px;
            height: 100%;
            object-fit: cover;
        }

        .brand-graid{
            width: calc(100% - 590px);
            display: flex;
            flex-wrap: wrap;

            & div{
                width: 25%;
                height: 25%;
                border: #f5f5f5 1px solid;
                display: flex;
                justify-content: center;
                align-items: center;
                
                &>img{
                    object-fit: cover;
                }
            }
        }
    }

}

.poster-container {
    display: flex;
    justify-content: space-between;
    flex-direction: column;
    margin-left: 240px;
    height: 100%;

    .poster-lantern {
        position: relative;
        display: flex;
        justify-content: center;

        & img {
            width: 100%;
            height: 400px;
            display: block;
            object-fit: cover;
        }
        
        .switch-cursor{
            position: absolute;
            align-self: center;
            display: flex;
            align-items: center;
            width: 40px;
            height: 100px;
            background-color: rgba(88, 88, 88, 0.3);
            cursor: pointer;

            & span{
                height: 40px;
                font-size: 40px;
                color: white;
                font-weight: lighter;
            }
        }

        .switch-cursor-active{
            background-color: rgba(88, 88, 88, 0.8);
        }

        .switch-cursor-left{
            left: 0px;
        }

        .switch-cursor-right{
            right: 0px;
        }


        .switch-bar {
            position: absolute;
            bottom: 40px;
            width: 35%;
            display: flex;
            justify-content: space-between;

            .switch-btn {
                width: 30%;
                height: 3px;
                background-color: rgba(219, 215, 215, 0.5);
                cursor: pointer;
            }

            .active-btn{
                background-color: white;
            }
        }
    }

    .poster-plate {
        display: flex;
        justify-content: space-between;

        & img {
            width: 33%;
            height: 140px;
            display: block;
        }
    }
}

.product-item {
    width: 20%;
    padding: 15px;
    display: flex;
    flex-direction: column;
    cursor: pointer;

    &:hover {
        outline: solid black 1px;
    }

    & span {
        margin: 4px 0;
    }

    a {
        text-decoration: none;
    }

}

.product-item img {
    height: 180px;
}

.product-item a {
    text-decoration: none;
}

.item-label {
    align-self: flex-start;
    background-color: red;
    color: white;
    font-size: 12px;
    padding: 0 3px;
}

.item-brand {
    font-size: 14px;
    font-weight: 600;
}

.item-name {
    font-size: 13px;
    display: block;
    color: black;

    &:hover {
        color: red;
    }
}

.item-price {
    font-size: 14px;
    display: block;
    color: black;
}

.item-image {
    width: 100%;
    height: 180px;
    position: relative;

    & img {
        width: 100%;
        height: 100%;
    }

    .car-button {
        position: absolute;
        top: 150px;
        height: 30px;
        line-height: 30px;
        background: rgba(0, 0, 0, .8);
        color: #fff;
        cursor: pointer;
        width: 100%;
        text-align: center;
        opacity: 0.8;
    }
}


.body-container {
    width: 100%;
    display: flex;
    justify-content: center;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}


footer {
    background-color: black;
    color: #c8c8c8;
    height: 100px;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
}
</style>
@/utils/login-user